#include "EditBox.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
#include <QPainter>
#include <QPixmap>


namespace UserManagementSystem {

EditBox::EditBox(QWidget* parent,const bool is_add_end)
    :FramelessWidget(parent),m_isEnd(is_add_end)
{

    //设置窗口的固定宽高
    this->setFixedHeight(50);

    //主布局
    m_mainLayout=new QHBoxLayout(this);

    //初始化左侧的iconlabel
    m_leftIconLabel=new QLabel();

    //初始化icon
    m_leftIcon=new QIcon();
    m_showIcon=new QIcon();
    m_noshowIcon=new QIcon();

    //初始化竖线
    m_vLine=new QFrame();
    m_vLine->setFrameShape(QFrame::VLine);
    m_vLine->setLineWidth(1);

    //初始化输入框
    m_inputEdit=new QLineEdit();


    //初始化hidebtn
    m_hideBtn=new QPushButton();
    m_hideBtn->setCheckable(true);//设置为选中or补选中状态

    //绑定信号槽函数
    connect(m_hideBtn,&QPushButton::clicked,this,&EditBox::On_hideBtn_clicked);


}

QIcon *EditBox::GetNoShowIconPointer() const
{
    return m_noshowIcon;
}

QIcon *EditBox::GetLeftIconPointer()const
{
    return m_leftIcon;
}

QIcon *EditBox::GetShowIconPointer()const
{
    return m_showIcon;
}

bool EditBox::ReadLeftIcon(const QString &path,QIcon * icon)
{
    //需要判断路径有效
    qDebug()<<__FUNCTION__;

    QFile file(path);
    if(!file.exists()){
        qDebug()<<"icon文件bu存在";
        return false;

    }

    //为传入的icon设置图标
    icon->addFile(path);

    if(m_leftIcon){
        qDebug()<<"icon文件存在";
        return true;
    }
    return false;
}

QLineEdit *EditBox::GetInputBox() const
{
    return m_inputEdit;
}



void EditBox::InitDefaultLayout()
{
    QPixmap pix=m_leftIcon->pixmap(25,25);
    m_leftIconLabel->setPixmap(pix);
    m_inputEdit->showMaximized();


    m_mainLayout->addWidget(m_leftIconLabel);
    m_mainLayout->addWidget(m_vLine);
    m_mainLayout->addWidget(m_inputEdit);

    //需要判断是否需要 一个选中按钮
    if(m_isEnd){
        m_mainLayout->addWidget(m_hideBtn);
        m_hideBtn->setIcon(*m_noshowIcon);
    }


    //布局设置完之后设置样式
    InitDefaultStyle();
}

void EditBox::InitDefaultStyle()
{

    m_inputEdit->setStyleSheet("QLineEdit { border: none; }");

    //m_hideBtn需要设置样式，样式为，不显示选中框
    m_hideBtn->setFlat(true);//去除边框

    m_hideBtn->setStyleSheet(R"(
    QPushButton {
        background-color: transparent;
        border: none;
    }
    QPushButton:checked {
        background-color: transparent;
    }
    QPushButton:focus {
        outline: none;
    }
)");


}

void EditBox::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing);

    QColor blue(255,255,255,255);//白色背景
    painter.setBrush(blue);
    painter.setPen(Qt::NoPen);
    painter.drawRoundedRect(this->rect(),20,20);
}

void EditBox::On_hideBtn_clicked()
{
    m_isPasswardVisible=!m_isPasswardVisible;
    if(m_isPasswardVisible){
        //密码可见
        m_inputEdit->setEchoMode(QLineEdit::Normal);
        m_hideBtn->setIcon(*m_showIcon);

    }else{

        m_inputEdit->setEchoMode(QLineEdit::Password);
        m_hideBtn->setIcon(*m_noshowIcon);

    }
}

};

